package com.erp.warehouse.clouderp_warehouse.service.impl.cost;

import com.erp.warehouse.clouderp_warehouse.mapper.cost.CostAdjustmentDetailMapper;
import com.erp.warehouse.clouderp_warehouse.mapper.cost.CostAdjustmentMapper;
import com.erp.warehouse.clouderp_warehouse.pojo.cost.CostAdjustment;
import com.erp.warehouse.clouderp_warehouse.pojo.cost.CostAdjustmentCreate;
import com.erp.warehouse.clouderp_warehouse.pojo.cost.CostAdjustmentDetail;
import com.erp.warehouse.clouderp_warehouse.pojo.cost.CostAdjustmentModify;
import com.erp.warehouse.clouderp_warehouse.service.cost.CostAdjustService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 2025/11/12 10:25
 * author: 向远洋
 */
@Service
@Transactional
public class CostAdjustServiceImpl implements CostAdjustService {
    @Autowired
    CostAdjustmentMapper mapper;
    @Autowired
    CostAdjustmentDetailMapper detailMapper;


    @Override
    public PageInfo<CostAdjustment> getPageInfo(Map<String, Object> params) {
        int pageNum = Integer.parseInt(params.get("pageNum").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        List<CostAdjustment> list = mapper.selectCostAdjustmentList(params);
        return new PageInfo<>(list);
    }

    @Override
    public void createWithItems(CostAdjustmentCreate request) {
        // 1. 插入主单
        CostAdjustment costAdjustment = request.getCostAdjustment();
        costAdjustment.setId(UUID.randomUUID().toString());
        List<CostAdjustmentDetail> items = request.getCostAdjustmentDetailList();
        mapper.insert(costAdjustment);
        //插入明细
        for (CostAdjustmentDetail item : items) {
            item.setId(UUID.randomUUID().toString());
            item.setOrderId(costAdjustment.getId());
        }
        if (items != null && !items.isEmpty()) detailMapper.insertCostAdjustmentDetailList(items);
    }

    @Override
    public CostAdjustmentCreate getCostAdjustmentWithDetail(String id) {
        CostAdjustment costAdjustment = mapper.selectCostAdjustmentById(id);
        List<CostAdjustmentDetail> list = detailMapper.selectCostAdjustmentDetailList(id);
        return new CostAdjustmentCreate(costAdjustment, list);
    }

    @Override
    public void modifyCostAdjustmentWithDetail(CostAdjustmentModify request) {
        // 执行删除操作
        List<String> delList = request.getDelList();
        if (!delList.isEmpty()) {
            int count = detailMapper.deleteCostAdjustmentDetailList(delList);
            if (count != delList.size()) {
                throw new RuntimeException("删除成本调整明细失败！");
            }
        }
        // 修改操作
        List<CostAdjustmentDetail> modList = request.getModList();
        if (!modList.isEmpty()) {
            for (CostAdjustmentDetail costAdjustmentDetail : modList) {
                int count = detailMapper.updateCostAdjustmentDetail(costAdjustmentDetail);
                if (count != modList.size()) {
                    throw new RuntimeException("修改成本调整明细失败！");
                }
            }
        }
        // 新增操作
        List<CostAdjustmentDetail> addList = request.getAddList();
        for (CostAdjustmentDetail costAdjustmentDetail : addList)
            costAdjustmentDetail.setId(UUID.randomUUID().toString());
        if (!addList.isEmpty()) {
            detailMapper.insertCostAdjustmentDetailList(addList);
        }
        // 判断是否需要修改主单
        boolean modified = request.getOrderModified();
        if (modified) {
            mapper.updateCostAdjustment(request.getCostAdjustment());
        }
    }

    @Override
    public void auditorCostAdjustmentWithDetail(CostAdjustmentCreate request) {
        
    }
}
